package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.SysBuildingRoom;
import org.dromara.system.domain.bo.SysBuildingRoomBo;
import org.dromara.system.domain.vo.SysBuildingRoomVo;
import org.dromara.system.mapper.SysBuildingRoomMapper;
import org.dromara.system.service.ISysBuildingRoomService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 房屋管理Service业务层处理
 *
 * @author LSL
 * @date 2024-05-27
 */
@RequiredArgsConstructor
@Service
public class SysBuildingRoomServiceImpl implements ISysBuildingRoomService {

    private final SysBuildingRoomMapper baseMapper;

    /**
     * 查询房屋管理
     */
    @Override
    public SysBuildingRoomVo queryById(Long roomId) {
        return baseMapper.selectVoById(roomId);
    }

    /**
     * 查询房屋管理列表
     */
    @Override
    public TableDataInfo<SysBuildingRoomVo> queryPageList(SysBuildingRoomBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysBuildingRoom> lqw = buildQueryWrapper(bo);
        Page<SysBuildingRoomVo> result = baseMapper.selectVoPageXML(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询房屋管理列表
     */
    @Override
    public List<SysBuildingRoomVo> queryList(SysBuildingRoomBo bo) {
        LambdaQueryWrapper<SysBuildingRoom> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysBuildingRoom> buildQueryWrapper(SysBuildingRoomBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysBuildingRoom> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRoomNum()), SysBuildingRoom::getRoomNum, bo.getRoomNum());
        lqw.eq(bo.getUnitId() != null, SysBuildingRoom::getUnitId, bo.getUnitId());
        lqw.eq(StringUtils.isNotBlank(bo.getLayer()), SysBuildingRoom::getLayer, bo.getLayer());
        lqw.eq(bo.getSection() != null, SysBuildingRoom::getSection, bo.getSection());
        lqw.eq(StringUtils.isNotBlank(bo.getApartment()), SysBuildingRoom::getApartment, bo.getApartment());
        lqw.eq(bo.getBuiltUpArea() != null, SysBuildingRoom::getBuiltUpArea, bo.getBuiltUpArea());
        lqw.eq(bo.getFeeCoefficient() != null, SysBuildingRoom::getFeeCoefficient, bo.getFeeCoefficient());
        lqw.eq(StringUtils.isNotBlank(bo.getState()), SysBuildingRoom::getState, bo.getState());
        lqw.eq(bo.getCommunityId() != null, SysBuildingRoom::getCommunityId, bo.getCommunityId());
        lqw.eq(StringUtils.isNotBlank(bo.getRoomType()), SysBuildingRoom::getRoomType, bo.getRoomType());
        lqw.eq(StringUtils.isNotBlank(bo.getRoomSubType()), SysBuildingRoom::getRoomSubType, bo.getRoomSubType());
        lqw.eq(bo.getRoomArea() != null, SysBuildingRoom::getRoomArea, bo.getRoomArea());
        lqw.eq(bo.getRoomRent() != null, SysBuildingRoom::getRoomRent, bo.getRoomRent());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysBuildingRoom::getStatus, bo.getStatus());
        lqw.eq(bo.getFloorId() != null, SysBuildingRoom::getFloorId, bo.getFloorId());
        return lqw;
    }

    /**
     * 新增房屋管理
     */
    @Override
    public Boolean insertByBo(SysBuildingRoomBo bo) {
        SysBuildingRoom add = MapstructUtils.convert(bo, SysBuildingRoom.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRoomId(add.getRoomId());
        }
        return flag;
    }

    /**
     * 修改房屋管理
     */
    @Override
    public Boolean updateByBo(SysBuildingRoomBo bo) {
        SysBuildingRoom update = MapstructUtils.convert(bo, SysBuildingRoom.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysBuildingRoom entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除房屋管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
